$.ajax({ 
    url:'/Role.do',
    method:'get',
    success:function(result){
        if(result&&result.length>0){
            for (var i=0;i<result.length;i++) {
                var val = result[i];
            $(`<tr><td>${val}</td></tr>`).appendTo('tbody');
            }
        }
    }
});


//添加角色
$('.add_role').click(function () {
    swal({
        allowOutsideClick:false,
        showCancelButton: true,
        cancelButtonColor: '#d33',
        title: '添加角色',
        html: `<input id="role" class="swal2-input">`,
        preConfirm: function () {
            return new Promise(function (resolve) {
                resolve(
                    $('#role').val(),
                )
            })
        },
        onOpen: function () {
            $('#role').focus()
        }
    }).then(function (result) {
        if(!result.value){return;}
        $.ajax({
            url: '/addRole.do',
            method: 'post',
            allowOutsideClick:false,
            data: `role=${$('#role').val()}`,
            success: function (result) {
                if (result == 'success') {
                    location.reload();
                } else if(result=='fail') {
                    swal('请完善添加信息', '添加失败', 'error');
                }else{
                    swal('用户已添加', '添加失败', 'error');
                }
            }
        });
    }).catch(swal.noop)
});